package controllers

import (
	"blog/models"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"os"
)

type MainController struct {
	beego.Controller
}

type Detail struct {
	Article models.Article
	Content string
	User models.Card
}

type NewArticle struct {
	Article models.Article
	User models.Card
}

func (c *MainController) Get() {
	o := orm.NewOrm()
	//获取轮播
	var banner []models.Banner
	qs := o.QueryTable(new(models.Banner))
	_,err := qs.All(&banner)
	if err != nil {
		beego.Info("轮播获取失败,错误信息:",err)
		return
	}
	c.Data["banner"] = banner

	card := models.Card{Id:1}
	err = o.Read(&card)
	if err != nil {
		beego.Info("我的名片获取失败,错误信息:",err)
		return
	}
	c.Data["card"] = card

	var menu []models.Menu
	menuQuery := o.QueryTable(new(models.Menu))
	_, err = menuQuery.OrderBy("sort").All(&menu)
	if err != nil {
		beego.Info("菜单获取失败,错误信息:",err)
		return
	}
	c.Data["menu"] = menu

	var article []models.Article
	articleQuery := o.QueryTable(new(models.Article))
	_, err = articleQuery.OrderBy("-id").All(&article)
	if err != nil {
		beego.Info("文章获取失败,错误信息:",err)
		return
	}
	var newArticle []NewArticle
	for _, data := range article{
		a := NewArticle{Article:data,User:card}
		newArticle = append(newArticle, a)
	}
	c.Data["article"] = newArticle

	//点击排行(10条)
	var ph []models.Article
	_, err = articleQuery.OrderBy("-read").Limit(10).All(&ph)
	if err != nil {
		beego.Info("获取排行文章失败,错误信息",err)
		return
	}
	c.Data["ph"] = ph
	c.TplName = "index/index.html"
}

//详情页
func (c*MainController) Info() {
	o := orm.NewOrm()
	var id, _ = c.GetInt("id")
	//获取菜单信息
	var menu []models.Menu
	menuQuery := o.QueryTable(new(models.Menu))
	_, err := menuQuery.OrderBy("sort").All(&menu)
	if err != nil {
		beego.Info("菜单获取失败,错误信息:",err)
		return
	}
	c.Data["menu"] = menu
	//查询文章
	var article = models.Article{Id:id}
	err = o.Read(&article)
	if err != nil {
		beego.Info("文章详情获取失败,错误信息:",err)
		return
	}
	//修改文章阅读数
	num := article.Read+1
	article.Read = num
	if _, err = o.Update(&article); err != nil {
		beego.Info("阅读量修改失败,错误信息:",err)
		return
	}

	f, err := os.Open(article.Article)
	if err != nil {
		beego.Info("文件打开失败,错误信息:", err)
		return
	}
	buf := make([]byte,1024*5)
	//读取文件内容
	_, err = f.Read(buf)
	if err != nil {
		beego.Info("读取文件失败,错误信息:", err)
		return
	}
	//关闭文件
	defer f.Close()

	//获取我的名片
	card := models.Card{Id:1}
	err = o.Read(&card)
	if err != nil {
		beego.Info("获取名片信息失败,错误信息:",err)
		return
	}
	info := Detail{Article:article,Content:string(buf),User:card}
	c.Data["info"] = info

	//上一篇文章
	var prev models.Article
	err = o.QueryTable("article").Filter("id__lt",id).OrderBy("-id").One(&prev)
	if err!=nil{
		beego.Info("未找到上一章")
	}
	c.Data["prev"] = prev
	//下一篇文章
	var next models.Article
	err = o.QueryTable("article").Filter("id__gt",id).OrderBy("id").One(&next)
	if err != nil{
		beego.Info("未找到下一章")
	}
	c.Data["next"] = next
	//点击排行(10条)
	var ph []models.Article
	query := o.QueryTable(new(models.Article))
	_, err = query.OrderBy("-read").Limit(10).All(&ph)
	if err != nil {
		beego.Info("获取排行文章失败,错误信息",err)
		return
	}
	c.Data["ph"] = ph

	c.TplName = "index/info.html"
}

//关于我
func (c*MainController) About() {
	o := orm.NewOrm()
	//获取菜单信息
	var menu []models.Menu
	menuQuery := o.QueryTable(new(models.Menu))
	_, err := menuQuery.OrderBy("sort").All(&menu)
	if err != nil {
		beego.Info("菜单获取失败,错误信息:",err)
		return
	}
	c.Data["menu"] = menu

	//获取我的名片
	var card = models.Card{Id:1}
	err = o.Read(&card)
	if err != nil {
		beego.Info("获取我的名片失败,错误信息:",err)
		return
	}
	c.Data["user"] = card
	c.TplName = "index/about.html"
}

//技术历程
func (c*MainController) List1() {
	o := orm.NewOrm()
	//获取菜单信息
	var menu []models.Menu
	menuQuery := o.QueryTable(new(models.Menu))
	_, err := menuQuery.OrderBy("sort").All(&menu)
	if err != nil {
		beego.Info("菜单获取失败,错误信息:",err)
		return
	}
	c.Data["menu"] = menu

	card := models.Card{Id:1}
	err = o.Read(&card)
	if err != nil {
		beego.Info("我的名片获取失败,错误信息:",err)
		return
	}

	var article []models.Article
	articleQuery := o.QueryTable(new(models.Article))
	_, err = articleQuery.Filter("label","技术历程").OrderBy("-id").All(&article)
	if err != nil {
		beego.Info("文章获取失败,错误信息:",err)
		return
	}

	var newArticle []NewArticle
	for _, data := range article{
		a := NewArticle{Article:data,User:card}
		newArticle = append(newArticle, a)
	}
	c.Data["article"] = newArticle

	//点击排行(10条)
	var ph []models.Article
	_, err = articleQuery.OrderBy("-read").Limit(10).All(&ph)
	if err != nil {
		beego.Info("获取排行文章失败,错误信息",err)
		return
	}
	c.Data["ph"] = ph

	c.TplName = "index/list1.html"
}

//精彩生活
func (c*MainController) List2() {
	o := orm.NewOrm()
	//获取菜单信息
	var menu []models.Menu
	menuQuery := o.QueryTable(new(models.Menu))
	_, err := menuQuery.OrderBy("sort").All(&menu)
	if err != nil {
		beego.Info("菜单获取失败,错误信息:",err)
		return
	}
	c.Data["menu"] = menu

	card := models.Card{Id:1}
	err = o.Read(&card)
	if err != nil {
		beego.Info("我的名片获取失败,错误信息:",err)
		return
	}

	var article []models.Article
	articleQuery := o.QueryTable(new(models.Article))
	_, err = articleQuery.Filter("label","精彩生活").OrderBy("-id").All(&article)
	if err != nil {
		beego.Info("文章获取失败,错误信息:",err)
		return
	}

	var newArticle []NewArticle
	for _, data := range article{
		a := NewArticle{Article:data,User:card}
		newArticle = append(newArticle, a)
	}
	c.Data["article"] = newArticle

	//点击排行(10条)
	var ph []models.Article
	_, err = articleQuery.OrderBy("-read").Limit(10).All(&ph)
	if err != nil {
		beego.Info("获取排行文章失败,错误信息",err)
		return
	}
	c.Data["ph"] = ph

	c.TplName = "index/list2.html"
}

//时间轴
func (c*MainController) Timer() {
	o := orm.NewOrm()
	//获取菜单信息
	var menu []models.Menu
	menuQuery := o.QueryTable(new(models.Menu))
	_, err := menuQuery.OrderBy("sort").All(&menu)
	if err != nil {
		beego.Info("菜单获取失败,错误信息:",err)
		return
	}
	c.Data["menu"] = menu

	var article []models.Article
	articleQuery := o.QueryTable(new(models.Article))
	_, err = articleQuery.OrderBy("id").All(&article)
	if err != nil {
		beego.Info("文章获取失败,错误信息:",err)
		return
	}
	c.Data["article"] = article
	c.TplName = "index/time.html"
}

//博客导航
func (c*MainController) Blog() {
	o := orm.NewOrm()
	//获取菜单信息
	var menu []models.Menu
	menuQuery := o.QueryTable(new(models.Menu))
	_, err := menuQuery.OrderBy("sort").All(&menu)
	if err != nil {
		beego.Info("菜单获取失败,错误信息:",err)
		return
	}
	c.Data["menu"] = menu
	c.TplName = "index/blog.html"
}

//点赞
func (c*MainController) Prise() {
	o := orm.NewOrm()
	id ,_ := c.GetInt("id")
	var article = models.Article{Id:id}
	err := o.Read(&article)
	if err != nil {
		beego.Info("文章信息获取失败,错误信息:",err)
		return
	}
	article.Live = article.Live+1
	if _, err = o.Update(&article); err != nil {
		beego.Info("阅读量修改失败,错误信息:",err)
		return
	}
	c.Data["json"] = map[string]interface{}{"code":200,"msg":"请求成功","data":""}
	c.ServeJSON()
	return
}